package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;

import com.itheima.ydd.dto.ArticleDto;


import com.itheima.ydd.entity.Article;

import com.itheima.ydd.entity.Cate;
import com.itheima.ydd.service.ArticleService;
import com.itheima.ydd.service.CateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.stream.Collectors;

/**
 * Create by Boller on 2022/2/26 17:56
*/


@SuppressWarnings("All")
@RestController
@RequestMapping("/ydd_article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CateService cateService;

    /**
     * 文章管理--分页查询
     * @param page
     * @param limit
     * @param start_time
     * @param end_time
     * @param title
     * @param cateid
     * @return
     */
    @GetMapping("/list")
    public ResultData list(Long start_time,Long end_time,String title,Integer cateid,Integer page,Integer limit) {

        Page<Article> pageInfo = new Page<>(page, limit);
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(title!=null,Article::getTitle,title);
        queryWrapper.eq(cateid!=null,Article::getCateId,cateid);
        queryWrapper.gt(start_time!=null,Article::getCreateDate,start_time).lt(end_time!=null,Article::getCreateDate,end_time);

        articleService.page(pageInfo,queryWrapper);

        Page<ArticleDto> articleDtoPage = new Page<>();
        // 拷贝数据
        BeanUtils.copyProperties(pageInfo, articleDtoPage, "records");

        List<ArticleDto> articleDtos = pageInfo.getRecords().stream().map(article -> {
            ArticleDto articleDto = new ArticleDto();
            BeanUtils.copyProperties(article,articleDto);
            Cate cate = cateService.getById(article.getCateId());
            if (cate!=null){
                articleDto.setCatename(cate.getCateName());
            }
            return articleDto;
        }).collect(Collectors.toList());
        articleDtoPage.setRecords(articleDtos);
        return ResultData.ok(PageResult.init(articleDtoPage));
    }

    /**
     * 文章管理--根据id删除
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData del(Integer id) {

        articleService.removeById(id);
        return ResultData.ok("删除成功");
    }

    /**
     * 文章管理--修改
     *
     * @param article
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody Article article) {

        articleService.updateById(article);
        return ResultData.ok("修改成功");
    }

    /**
     * 文章管理--根据id查询单个数据
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(Integer id) {
        return ResultData.ok(articleService.getById(id));
    }

    /**
     * 文章管理--添加
     *
     * @param article
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody Article article) {
        articleService.save(article);
        return ResultData.ok("添加成功");
    }
}
